#ifndef LOG_LOGFILE_H
#define LOG_LOGFILE_H

#include "utility/Noncopyable.h"

#include <memory>
#include <mutex>
#include <string>

class AppendFile;

class LogFile final {
public:
    LogFile(std::size_t rollSize, std::string prefix, 
        std::string suffix = ".log", bool rename = true, bool threadSafe = true);
    ~LogFile();

    const std::string &filename() const;

    void append(const char *buffer, std::size_t length);
    void flush();

private:
    NONCOPYABLE(LogFile);

    void _append_flush();
    void _append_without_lock(const char *buffer, std::size_t length);
    void _roll();

    std::mutex _mutex;
    const std::string _prefix;
    const std::string _suffix;
    std::unique_ptr<AppendFile> _file;
    time_t _startMoment;
    time_t _lastRollMoment;
    time_t _lastFlushMoment;
    const std::size_t _rollSize;
    int _count;
    const bool _rename;
    const bool _threadSafe;
};

#endif